package com.xyazm.order.statemachine.action.order.pay;

import com.xyazm.common.enums.OrderStatusChangeEnum;
import com.xyazm.order.dao.OrderInfoDAO;
import com.xyazm.order.dao.OrderPaymentDetailDAO;
import com.xyazm.order.domain.dto.OrderInfoDTO;
import com.xyazm.order.domain.entity.OrderInfoDO;
import com.xyazm.order.domain.entity.OrderPaymentDetailDO;
import com.xyazm.order.statemachine.action.OrderStateAction;
import com.xyazm.pay.domain.dto.PayOrderDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 订单预支付 action
 * @author xuyou
 * @date 2023年02月06日 21:45
 */
@Slf4j
@Component
public class PrePayOrderAction extends OrderStateAction<PayOrderDTO> {

    @Autowired
    private OrderInfoDAO orderInfoDAO;

    @Autowired
    private OrderPaymentDetailDAO orderPaymentDetailDAO;

    @Override
    public OrderStatusChangeEnum event() {
        return OrderStatusChangeEnum.ORDER_PREPAY;
    }

    @Override
    protected OrderInfoDTO onStateChangeInternal(OrderStatusChangeEnum event, PayOrderDTO context) {
        // 更新订单表与支付信息表
        updateOrderPaymentInfo(context);
        // 返回null，不会发送标准订单变更消息
        return null;
    }

    /**
     * 更新订单表与支付信息表
     * @author xuyou
     * 2022/6/16 0016 22:00
     * @param payOrderDTO
     */
    private void updateOrderPaymentInfo(PayOrderDTO payOrderDTO) {
        // 提取业务参数
        String orderId = payOrderDTO.getOrderId();
        Integer payType = payOrderDTO.getPayType();
        String outTradeNo = payOrderDTO.getOutTradeNo();
        Date payTime = new Date();

        // 更新主订单支付信息
        updateMasterOrderPaymentInfo(orderId,payType,payTime,outTradeNo);
        // 更新子订单支付信息
        updateSubOrderPaymentInfo(orderId, payType, payTime, outTradeNo);
    }

    /**
     * 更新主订单支付信息
     * @param orderId
     * @param payType
     * @param payTime
     * @param outTradeNo
     */
    private void updateMasterOrderPaymentInfo(String orderId, Integer payType, Date payTime, String outTradeNo) {
        List<String> orderIds = Collections.singletonList(orderId);
        // 更新订单表支付信息
        updateOrderInfo(orderIds, payType, payTime);
        // 更新支付明细信息
        updateOrderPaymentDetail(orderIds, payType, payTime, outTradeNo);
    }


    /**
     * 更新子订单支付信息
     * @param orderId
     * @param payType
     * @param payTime
     * @param outTradeNo
     */
    private void updateSubOrderPaymentInfo(String orderId, Integer payType, Date payTime, String outTradeNo) {
        // 判断是否存在子订单，不存在则不处理
        List<String> subOrderIds = orderInfoDAO.listSubOrderIds(orderId);
        if (subOrderIds == null || subOrderIds.isEmpty()) {
            return;
        }
        // 更新子订单支付信息
        updateOrderInfo(subOrderIds, payType, payTime);
        // 更新子订单支付明细信息
        updateOrderPaymentDetail(subOrderIds, payType, payTime, outTradeNo);
    }


    /**
     * 更新订单信息表
     * @param orderIds
     * @param payType
     * @param payTime
     */
    private void updateOrderInfo(List<String> orderIds, Integer payType, Date payTime) {
        if(orderIds == null) {
            return;
        }
        OrderInfoDO orderInfoDO = new OrderInfoDO();
        orderInfoDO.setPayType(payType); // 支付方式 10:微信支付 20:支付宝支付
        orderInfoDO.setPayTime(payTime); // 支付时间
        if(orderIds.size() == 1) {
            orderInfoDAO.updateByOrderId(orderInfoDO, orderIds.get(0));
        } else {
            orderInfoDAO.updateBatchByOrderIds(orderInfoDO, orderIds);
        }

    }

    /**
     * 更新支付明细信息
     * @author xuyou
     * 2022-12-10 20:10
     * @param orderIds
     * @param payType
     * @param payTime
     * @param outTradeNo
     */
    private void updateOrderPaymentDetail(List<String> orderIds, Integer payType, Date payTime, String outTradeNo) {
        if(orderIds == null) {
            return;
        }
        OrderPaymentDetailDO orderPaymentDetailDO = new OrderPaymentDetailDO();
        orderPaymentDetailDO.setPayType(payType); // 支付方式 10:微信支付 20:支付宝支付
        orderPaymentDetailDO.setPayTime(payTime); // 支付时间
        orderPaymentDetailDO.setOutTradeNo(outTradeNo); // 支付系统交易流水号
        if(orderIds.size() == 1) {
            orderPaymentDetailDAO.updateByOrderId(orderPaymentDetailDO, orderIds.get(0));
        } else {
            orderPaymentDetailDAO.updateBatchByOrderIds(orderPaymentDetailDO, orderIds);
        }
    }

}
